package cn.com.darly.vpm.mobile.session;

import cn.com.darly.vpm.framework.systemlog.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/** Session 超时检测器
 * Author : ZhangYuHui
 * Date : 2017/12/5
 */
@Component
public class MobileSecuritySchedule {
    public Logger logger = new Logger(getClass());
    //保存验证码，验证超时的集合
    public static Map<String, MobileSecurityData> MOBILESECURITY = new HashMap<>();

    // 每15秒执行一次
    @Scheduled(cron = "0/30 * * * * ?")
    public void reportCurrentByCron() {
        MobileSecurityData  sd = null;
        for (String key : MOBILESECURITY.keySet()) {
             sd = MOBILESECURITY.get(key);
            //单位分钟10分钟后失效即可
            if (isTimeOut(sd.getCreateDate(), 3)) {
                logger.info(key+"验证编码超时");
                MOBILESECURITY.remove(key);
            }
        }

    }
    /**
     * 判断是否超时
     * @return boolean
     */
    private boolean isTimeOut(Date loginDate, int timeOut) {
        Calendar c = Calendar.getInstance();
        c.setTime(loginDate);
        c.add(Calendar.MINUTE, timeOut);
        //当前登录
        if (c.getTime().before( new Date())) {
            return true;
        }
        return false;
    }
}
